<template>
	<view class="content b-t">

		<!-- <tui-swipe-action :actions="actions" v-for="(item,index) in addressList" :key="index" @click="checkAddress(item)">
			<template v-slot:content>
				<view class="list b-b">
					<view class="wrapper">
						<view class="u-box">
							<text class="name">{{item.name}}</text>
							<text class="mobile">{{item.tel}}</text>
							<text v-if="item.isDefault" class="tag">默认</text>
						</view>
						<view class="address-box">
							<text class="address">{{item.province}} {{item.city}} {{item.county}} {{item.addressDetail}}</text>
						</view>
					</view>
					<text class="yticon icon-bianji" @click.stop="addAddress('edit', item)"></text>
				</view>
			</template>
		</tui-swipe-action> -->

		<view class="list b-b" v-for="(item,index) in addressList" :key="index" @click="checkAddress(item)">
			<view class="wrapper">
				<view class="u-box">
					<text class="name">{{item.name}}</text>
					<text class="mobile">{{item.tel}}</text>
					<text v-if="item.isDefault" class="tag">默认</text>
				</view>
				<view class="address-box">
					<text class="address">{{item.province}} {{item.city}} {{item.county}} {{item.addressDetail}}</text>
				</view>
			</view>
			<text class="yticon icon-bianji" @click.stop="addAddressEdit('edit', item)"></text>
		</view>


		<!-- <button class="add-btn" @click="addAddress('add')"> + 新增地址</button> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				source: 0,
				userId: "",
				addressList: [],
				actions: [{
					name: '删除',
					color: '#fff',
					fontsize: 30, //单位rpx
					width: 70, //单位px
					background: '#FD3B31',
				}],
			}
		},
		
		onLoad: function(option) {
			let userInfo = uni.getStorageSync("userInfo")

			this.source = option.source;

			this.userId = userInfo.id

			this.addressData();

		},
		
		// 刷新页面
		onShow: function() {
			this.addressData()
		},
		
		//获取导航栏右侧的按钮事件
		onNavigationBarButtonTap(e) {
			let type = "add"
			uni.navigateTo({
				url: `/pages/address/addressManage?userId=${this.userId}&type=${type}`
			})
		
		},
		
		methods: {
			// 初始化获取地址列表数据
			addressData() {
				this.tui.request('/app/address/list', 'GET', {
							userId: this.userId,
						},
						false, false, false, )
					.then(res => {
						if (res.errno == 0) {
							this.addressList = res.data.list
							this.cartTotal = res.data.cartTotal

						} else {
							console.log(res)
							this.tui.toast(res.errmsg)
						}
					}).catch(err => {
						console.log(err)
					})
			},

			// 选择地址
			checkAddress(item) {
				console.log(item)
				this.$api.prePage().addressData = item;
				uni.navigateBack({})
			},

			// 新增地址
			addAddress(type, item) {
				uni.navigateTo({
					url: `/pages/address/addressManage?userId=${this.userId}&type=${type}`
				})
			},

			// 修改地址
			addAddressEdit(type, item) {
				uni.navigateTo({
					url: `/pages/address/addressManage?userId=${this.userId}&type=${type}&data=${JSON.stringify(item)}`
				})
			},

			//添加或修改成功之后回调    直接刷新地址列表
			refreshList(data, type) {
				console.log(data, type);

				this.addressData();
			}

		}
	}
</script>

<style lang='scss'>
	page {
		padding-bottom: 120upx;
	}

	.content {
		position: relative;
	}

	.list {
		display: flex;
		align-items: center;
		padding: 20upx 30upx;
		;
		background: #fff;
		position: relative;
	}

	.wrapper {
		display: flex;
		flex-direction: column;
		flex: 1;
	}

	.address-box {
		display: flex;
		align-items: center;
		margin-top: 16upx;

		.address {
			font-size: 13px;
			color: $font-color-dark;
		}
	}

	.u-box {
		font-size: 14px;
		color: #333232;

		.name {
			margin-right: 30upx;
		}
	}

	.tag {
		font-size: 14px;
		color: $base-color;
		margin-left: 12upx;
		background: #fffafb;
		border: 1px solid #ffb4c7;
		border-radius: 4upx;
		padding: 4upx 10upx;
		line-height: 1;
	}

	.icon-bianji {
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
	}

	.add-btn {
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 16upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		background-color: #D14798;
		border-radius: 45px;
		box-shadow: 1px 2px 5px rgba(250, 250, 250, 0.4);
	}
</style>
